package com.yunguanjia.base.controller;

import com.yunguanjia.base.domain.dto.Medicines;
import com.yunguanjia.base.service.IMedicinesService;
import com.yunguanjia.common.annotation.Log;
import com.yunguanjia.common.core.controller.BaseController;
import com.yunguanjia.common.core.domain.AjaxResult;
import com.yunguanjia.common.core.page.TableDataInfo;
import com.yunguanjia.common.enums.BusinessType;
import com.yunguanjia.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 医药信息Controller
 *
 * @author xzh
 * @date 2024-11-16
 */
@RestController
@RequestMapping("/base/medicines")
public class MedicinesController extends BaseController {
    @Autowired
    private IMedicinesService medicinesService;

/**
 * 查询医药信息列表
 */
@PreAuthorize("@ss.hasPermi('base:medicines:list')")
@GetMapping("/list")
    public TableDataInfo list(Medicines medicines) {
        startPage();
        List<Medicines> list = medicinesService.selectMedicinesList(medicines);
        return getDataTable(list);
    }

    /**
     * 获取医药信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('base:medicines:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(medicinesService.getById(id));
    }

    /**
     * 新增医药信息
     */
    @PreAuthorize("@ss.hasPermi('base:medicines:add')")
    @Log(title = "医药信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Medicines medicines) {
        return toAjax(medicinesService.save(medicines));
    }

    /**
     * 修改医药信息
     */
    @PreAuthorize("@ss.hasPermi('base:medicines:edit')")
    @Log(title = "医药信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Medicines medicines) {
        return toAjax(medicinesService.updateById(medicines));
    }

    /**
     * 删除医药信息
     */
    @PreAuthorize("@ss.hasPermi('base:medicines:remove')")
    @Log(title = "医药信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(medicinesService.removeByIds(Arrays.asList(ids)));
//        return toAjax(medicinesService.removeById(ids));
    }

    /**
     * 导出医药信息列表
     */
    @PreAuthorize("@ss.hasPermi('base:medicines:export')")
    @Log(title = "医药信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Medicines medicines) {
        List<Medicines> list = medicinesService.selectMedicinesList(medicines);
        ExcelUtil<Medicines> util = new ExcelUtil<Medicines>(Medicines. class);
        util.exportExcel(response, list, "医药信息数据");
    }


    /**
     * 导入文件模板
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Medicines> util = new ExcelUtil<>(Medicines. class);
        util.importTemplateExcel(response, "医药信息数据");
    }
}
